
public class Merge2SortedArray {
	
	public static int[] merge(int[] arr1, int[]arr2){
		int[] result = new int[arr1.length+arr2.length];
		
		int i = 0;
		int j = 0;
		int k = 0;
		while(i < arr1.length && j < arr2.length){
			if(arr1[i] < arr2[j]){
				result[k] = arr1[i];
				i++;
			}
			else{
				result[k] = arr2[j];
				j++;
			}
			
			k++;
		}
		
		while(i < arr1.length){
			result[k] = arr1[i];
			i++;
			k++;
		}
		
		while(j < arr2.length){
			result[k] = arr2[j];
			j++;
			k++;
		}
		
		return result;
		
	}
	
	public static String toString(int[] arr){
		if(arr.length == 0 || arr == null){
			return "This array is empty.";
		}
		
		StringBuffer sb = new StringBuffer();
		for(int i : arr){
			sb.append(i);
		}
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr1 = {1,3,4,5,7,8};
		int[] arr2 = {2,6};
		int[] result = Merge2SortedArray.merge(arr1, arr2);
		System.out.print(Merge2SortedArray.toString(result));
	}

}
